/**   
 * @Title: UserDaoImplementation.java 
 * @Package cn.edu.cdtu.healthserver.dao.impl 
 * @Description: TODO 
 * @author  
 * @date 
 * @version V0.0   
 */
package cn.edu.cdtu.healthserver.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import cn.edu.cdtu.healthserver.dao.SQLToken;
import cn.edu.cdtu.healthserver.dao.UserDao;
import cn.edu.cdtu.healthserver.domain.UserInformation;
import cn.edu.cdtu.healthserver.utils.C3P0Util;

/** 
 * @ClassName: UserDaoImplementation 
 * @Description: 关于用户的数据库查询操作。
 * @author 
 * @date 
 *  
 */
public class UserDaoImplementation implements UserDao {
	
	//定义字段
	private static final String TABLE_STRING = " user ";
	private static final String ATTRIBUTE_ID_STRING = " id ";
	private static final String ATTRIBUTE_ACCOUNT_STRING = " account ";
	private static final String ATTRIBUTE_PASSWORD_STRING = " password ";
	private static final String ATTRIBUTE_NAME_STRING = " name ";
	
	private QueryRunner queryRunner =
			new QueryRunner(C3P0Util.getDataSource());

	/* (non-Javadoc)
	 * <p>Title: queryUser</p> 
	 * <p>Description: 从 user表中查询所有数据项
	 * @return 
	 * @see cn.edu.cdtu.healthserver.dao.UserDao#queryUser()
	 */
	@Override
	public List<UserInformation> queryUser() {
		// TODO Auto-generated method stub
		//SQL语言
		final String sql=""
				+ SQLToken.SELECT_STRING + "*"
				+ SQLToken.FROM_STRING + TABLE_STRING;
		//执行数据库查询。
		try 
		{
			return queryRunner.query(sql,
					new BeanListHandler<UserInformation>(UserInformation.class));
		} 
		catch (SQLException e) 
		{
			throw new RuntimeException();
		}
	}

	/* (non-Javadoc)
	 * <p>Title: add</p> 
	 * <p>Description:向user表中添加用户数据项。
	 * @param object
	 * @return 
	 * @see cn.edu.cdtu.healthserver.dao.ObjectQueryDao#add(java.lang.Object)
	 */
	@Override
	public boolean add(UserInformation object) {
		// TODO Auto-generated method stub
		//SQL语句
		final String sqlString = ""
				+ SQLToken.INSERT_INTO_STRING
				+ TABLE_STRING
				+ "("
				+ ATTRIBUTE_ACCOUNT_STRING + ","
				+ ATTRIBUTE_PASSWORD_STRING + ","
				+ ATTRIBUTE_NAME_STRING
				+ ")"
				+ SQLToken.VALUES_STRING + "(?,?,?)";
		//把获取的用户数据封装成Object对象，便于存储。
		Object[] paramsObject = new Object[]{
				object.getAccount(),object.getPassword(),object.getName()
		};
		//执行数据库插入操作。
		try {
			int result = queryRunner.update(sqlString, paramsObject);
			if(result > 0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	/* (non-Javadoc)
	 * <p>Title: update</p> 
	 * <p>Description: 更新数据库信息 
	 * @param object
	 * @return 
	 * @see cn.edu.cdtu.healthserver.dao.ObjectQueryDao#update(java.lang.Object)
	 */
	@Override
	public boolean update(UserInformation object) {
		// TODO Auto-generated method stub
		//更新数据库的SQL语句
		final String sqlString = ""
				+ SQLToken.UPDATE_STRING
				+ TABLE_STRING
				+ SQLToken.SET_STRING
				+ ATTRIBUTE_ACCOUNT_STRING + "=?,"
				+ ATTRIBUTE_PASSWORD_STRING + "=?,"
				+ ATTRIBUTE_NAME_STRING + "=?"
				+ SQLToken.WHERE_STRING
				+ ATTRIBUTE_ID_STRING + "=?";
		//将服务器获取的用户信息封装成Object对象，便于存储。
		Object[] paramsObject = new Object[]{
				object.getAccount(),object.getPassword(),object.getName(),
				object.getId()
		};
		//执行SQL数据库更新操作
		try {
			int result = queryRunner.update(sqlString, paramsObject);
			if(result > 0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	/* (non-Javadoc)
	 * <p>Title: delete</p> 
	 * <p>Description: 数据库用户信息删除操作
	 * @param object
	 * @return 
	 * @see cn.edu.cdtu.healthserver.dao.ObjectQueryDao#delete(java.lang.Object)
	 */
	@Override
	public boolean delete(UserInformation object) {
		// TODO Auto-generated method stub
		//数据库删除SQL语句
		final String sqlString = ""
				+ SQLToken.DELETE_FROM_STRING
				+ TABLE_STRING
				+ SQLToken.WHERE_STRING
				+ ATTRIBUTE_ID_STRING + "= ?";
		//将user表中要删除的数据项的ID封装为Object对象。
		Object[] paramsObject = new Object[]{
			object.getId()	
		};
		//这些删除SQL语句
		try {
			int result = queryRunner.update(sqlString, paramsObject);
			if(result > 0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

}
